package controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import pojo.Department;
import pojo.GuaHao;
import pojo.Result;
import service.DepartmentService;
import service.GuaHaoService;

import java.util.List;
@RestController
//1
@RequestMapping("/guahao")
@Api(tags = "用户端挂号", description = "挂号相关的操作")
public class GuaHaoController {
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private GuaHaoService guaHaoService;
    @GetMapping("/list")
    @ApiOperation(value = "获取部门列表", notes = "获取所有部门的基本信息列表")
    @ApiResponses({
            @ApiResponse(code = 200, message = "操作成功", response = Department.class, responseContainer = "List"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    public List<Department> getAllDepartments() {
        return departmentService.selectAllDepartments();
    }

    @GetMapping("/doctorslist")
    @ApiOperation(value = "获取科室的全部医生列表", notes = "根据科室ID获取挂号医生列表")
    public Result<List<GuaHao>> getDoctorsByDepartmentId(@RequestParam Integer departmentId) {
        List<GuaHao> doctors = guaHaoService.getDoctorsByDepartmentId(departmentId);
        return Result.success(doctors);
    }

    @GetMapping("/doctorDetail")
    @ApiOperation(value = "获取选择的医生详情", notes = "根据docId获取挂号医生详情")
    public Result<GuaHao> getDoctorDetailByDocId(@RequestParam String docId) {
        GuaHao doctor = guaHaoService.getDoctorDetailByDocId(docId);
        return Result.success(doctor);
    }

    @GetMapping("/availableSlots")
    @ApiOperation(value = "查询该医生某天可挂号数量", notes = "根据docId和scheduleId查询医生某天上午下午可挂号数量")
    public Result<GuaHao> getAvailableSlotsByDocIdAndScheduleId(@RequestParam String docId, @RequestParam String scheduleId) {
        GuaHao slots = guaHaoService.getAvailableSlotsByDocIdAndScheduleId(docId, scheduleId);
        return Result.success(slots);
    }

    @GetMapping("/doctorsWithAvailableSlots")
    @ApiOperation(value = "根据科室和日期查询有号医生列表", notes = "根据科室ID和日期查询有号医生列表")
    public Result<java.util.List<GuaHao>> getDoctorsWithAvailableSlotsByDepartmentAndDate(@RequestParam Integer departmentId, @RequestParam String queryDate) {
        java.util.List<GuaHao> doctors = guaHaoService.getDoctorsWithAvailableSlotsByDepartmentAndDate(departmentId, queryDate);
        return Result.success(doctors);
    }
    
    @GetMapping("/registrationConfirmation")
    @ApiOperation(value = "确认挂号信息", notes = "根据医生ID和排班ID获取挂号确认信息")
    @ApiResponses({
            @ApiResponse(code = 200, message = "操作成功", response = GuaHao.class),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    public Result<GuaHao> getRegistrationConfirmation(@RequestParam String docId, @RequestParam String scheduleId) {
        GuaHao confirmation = guaHaoService.getRegistrationConfirmation(docId, scheduleId);
        return Result.success(confirmation);
    }

    @GetMapping("/appointmentsByUserId")
    @ApiOperation(value = "根据用户ID查询预约信息", notes = "根据userId查询该用户的所有预约信息")
    public Result<List<GuaHao>> getAppointmentsByUserId(@RequestParam Integer userId) {
        List<GuaHao> appointments = guaHaoService.getAppointmentsByUserId(userId);
        return Result.success(appointments);
    }

    @PostMapping("/register")
    @ApiOperation(value = "挂号", notes = "挂号成功时插入patient_doctor表")
    public Result<String> registerPatientDoctor(@RequestBody GuaHao guaHao) {
        int result = guaHaoService.registerPatientDoctor(guaHao);
        if (result > 0) {
            return Result.success("挂号成功");
        } else {
            return Result.fail("挂号失败");
        }
    }

    @PostMapping("/deleteRegistration")
    @ApiOperation(value = "删除挂号信息", notes = "根据id删除patient_doctor表中的挂号信息")
    public Result<String> deletePatientDoctorById(@RequestParam Integer id) {
        int result = guaHaoService.deletePatientDoctorById(id);
        if (result > 0) {
            return Result.success("删除成功");
        } else {
            return Result.fail("删除失败");
        }
    }
}
